package cn.fanqinghong.ymcc.mq.producer;

import cn.fanqinghong.ymcc.domain.MediaFile;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * FAN
 * 2022/7/24  9:23
 */
@Component
public class MediaHandlerProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送消息是否成功
     * @param mediaFile
     */
    public boolean sendMessage(MediaFile mediaFile){
        //以下操作为了方便消费方重新将message封装成MediaFile对象
        String mediaFileJsonStr = JSON.toJSONString(mediaFile);
        SendResult sendResult = rocketMQTemplate.syncSend("media-topic:media-tags", mediaFileJsonStr);
        SendStatus sendStatus = sendResult.getSendStatus();
        //枚举对象和返回状态相等true不相等false
        return sendStatus == SendStatus.SEND_OK;
    }
}
